home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / bootstrap / socket.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  1.8 KB  |  109 lines

  1.  
  2. /*
  3.  *  BACKENDSOCK.C
  4.  *
  5.  *  Backend socket communications routines
  6.  */
  7.  
  8. #include <errno.h>
  9. #include "tmp/c.h"
  10. #include "utils/log.h"
  11.  
  12. RcsId("$Header: /private/postgres/src/bootstrap/RCS/socket.c,v 1.2 1991/11/06 04:27:09 mer Exp $");
  13.  
  14. #define MAXQUERYSIZE    8192
  15.  
  16. static int PortFd = -1;        /* socket fd                  */
  17. static char InBuf[8192];    /* communications buffer, arbitrary size */
  18. static int InIdx;        /* current index             */
  19. static int InLen;        /* current length
  20.  
  21. static char OutBuf[8192];    /* output buffer, arbitrary size     */
  22. static int OutIdx;
  23.  
  24. extern int DebugMode;        /* from backendsup.c             */
  25.  
  26. /*
  27.  * function prototypes this file only
  28.  */
  29. void SetPQSocket ARGS((int fd ));
  30. char GetPQChar ARGS((void ));
  31. int GetPQInt4 ARGS((void ));
  32. void GetPQStr ARGS((char *buf ));
  33. void LoadSocketBuf ARGS((void ));
  34.  
  35. void
  36. SetPQSocket(fd)
  37. int fd;
  38. {
  39.     PortFd = fd;
  40.     InIdx = 0;
  41.     InLen = 0;
  42.     OutIdx= 0;
  43. }
  44.  
  45. char
  46. GetPQChar()
  47. {
  48.     if (InIdx == InLen)
  49.     LoadSocketBuf();
  50.     if (InIdx == InLen)
  51.     return(0);
  52.     return(InBuf[InIdx++]);
  53. }
  54.  
  55. int 
  56. GetPQInt4()
  57. {
  58.     int i;        /* index, count 4 bytes */
  59.     int r = 0;        /* result        */
  60.  
  61.     for (i = 0; i < 4; ++i) {
  62.     if (InIdx == InLen)
  63.         LoadSocketBuf();
  64.     if (InIdx == InLen)
  65.         elog(FATAL, "GetPQInt4.read error");
  66.     r = (r << 8) | InBuf[InIdx++];
  67.     }
  68.     return(r);
  69. }
  70.  
  71. void
  72. GetPQStr(buf)
  73. char *buf;
  74. {
  75.     int i;
  76.  
  77.     for (i = 0; i < MAXQUERYSIZE; ++i) {
  78.     if (InIdx == InLen)
  79.         LoadSocketBuf();
  80.     if (InIdx == InLen)
  81.         elog(FATAL, "GetPQStr.read error");
  82.     buf[i] = InBuf[InIdx++];
  83.     if (buf[i] == 0)
  84.         break;
  85.     }
  86.     if (i == MAXQUERYSIZE)
  87.     elog(FATAL, "GetPQStr.len error");
  88. }
  89.  
  90. void
  91. LoadSocketBuf()
  92. {
  93.     int n;
  94.     extern int errno;
  95.  
  96.     for (;;) {
  97.     n = read(PortFd, InBuf, sizeof(InBuf));
  98.     if (n > 0)
  99.         break;
  100.     if (n < 0) {
  101.         if (errno != EINTR)
  102.         elog(FATAL, "LoadSocketBuf.read error %d", errno);
  103.     }
  104.     }
  105.     InIdx = 0;
  106.     InLen = n;
  107. }
  108.  
  109.